//
//  DestinationCityManager.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "DestinationCityManager.h"

@implementation DestinationCityManager

- (void)RequestDataByStringReloadWithBlock:(void(^)(NSMutableArray *array))block urlString:(NSString *)urlString
{
    _app = [[AppDelegate alloc] init];                                                                                                                      if ([_app internetReachableOrNot]) {
    NSString *url = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30.0];
    self.tempArray = [NSMutableArray array];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
   NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
   
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (error) {
            NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
        } else {
            NSDictionary *tempDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSMutableArray *tempArr = [tempDict valueForKey:@"data"];
            for (NSDictionary *dic in tempArr) {
                DestinationCityModel *model = [[DestinationCityModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.tempArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                block(self.tempArray);
            });
        }
    }];
    [dataTask resume];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"🙅 您的当前网络中断或网络信号差, 请检查网络后重新进入!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
}



@end


